package com.alibaba.citrus.ots.application.report.ability.utils;

import com.epoch.app.otsapplicationreportsdk.model.dto.SubtotalViewVO;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/27 17:21
 */
public class SubtotalViewVOUtil {

    public final static int TEN_THOUSAND = 10000;
    public final static int ONE_HUNDRED_THOUSAND = 100000;
    public final static int HUNDRED_MILLION = 100000000;
    public final static int BILLION = 1000000000;

    public final static String YUAN = "元";
    public final static String JIAN = "件";
    public final static String PERCENT = "%";

    public static SubtotalViewVO getSubtotalVODeltaUnsigned(String label, String baseUnit, BigDecimal number){
        int scale = 2;
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit(baseUnit);
            return vo;
        }
        //小于10万
        if(number.abs().compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND)) < 0 ){
            String[] array = decimalStrToArray(number.toString());
            vo.setValue(array[0]);
            vo.setDecimal("."+array[1]);
            vo.setUnit(baseUnit);
            return vo;
        }
        //大于10万小于10亿
        if(number.abs().compareTo(new BigDecimal(BILLION)) < 0){
            String[] array = decimalStrToArray(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP).toString());
            vo.setValue(array[0]);
            vo.setDecimal("."+array[1]);
            vo.setUnit("万"+baseUnit);
            return vo;
        }
        String[] array = decimalStrToArray(number.divide(new BigDecimal(HUNDRED_MILLION),scale,BigDecimal.ROUND_HALF_UP).toString());
        vo.setValue(array[0]);
        vo.setDecimal("."+array[1]);
        vo.setUnit("亿"+baseUnit);
        return vo;
    }

    public static SubtotalViewVO getSimpleSubtotalVO(String label, String baseUnit, BigDecimal number){
        int scale = 2;
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit("万"+baseUnit);
            return vo;
        }
        String[] array = decimalStrToArray(number.toString());
        vo.setValue(array[0]);
        vo.setDecimal("."+array[1]);
        vo.setUnit("万"+baseUnit);
        return vo;
    }

    public static SubtotalViewVO getSubtotalVODeltaNoFormat(String label, String baseUnit, BigDecimal number){
        int scale = 2;
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit(baseUnit);
            return vo;
        }
        //小于10万
        if(number.abs().compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND)) < 0 ){
            String[] array = decimalStrToArray(number.toString());
            vo.setValue(new BigDecimal(array[0]).toString());
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit(baseUnit);
            return vo;
        }
        //大于10万小于10亿
        if(number.abs().compareTo(new BigDecimal(BILLION)) < 0){
            String[] array = decimalStrToArray(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP).toString());
            vo.setValue(new BigDecimal(array[0]).toString());
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit("万"+baseUnit);
            return vo;
        }
        String[] array = decimalStrToArray(number.divide(new BigDecimal(HUNDRED_MILLION),scale,BigDecimal.ROUND_HALF_UP).toString());
        vo.setValue(new BigDecimal(array[0]).toString());
        vo.setDecimal(array.length<=1?null:"."+array[1]);
        vo.setUnit("亿"+baseUnit);
        return vo;
    }

    public static SubtotalViewVO getSubtotalVOFormat(String label, String baseUnit, BigDecimal number){
        DecimalFormat df = new DecimalFormat("###,###");
        int scale = 2;
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit(baseUnit);
            return vo;
        }
        //小于10万
        if(number.abs().compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND)) < 0 ){
            String[] array = decimalStrToArray(number.toString());
            vo.setValue(df.format(new BigDecimal(array[0])));
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit(baseUnit);
            return vo;
        }
        //大于10万小于10亿
        if(number.abs().compareTo(new BigDecimal(BILLION)) < 0){
            String[] array = decimalStrToArray(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP).toString());
            vo.setValue(df.format(new BigDecimal(array[0])));
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit("万"+baseUnit);
            return vo;
        }
        String[] array = decimalStrToArray(number.divide(new BigDecimal(HUNDRED_MILLION),scale,BigDecimal.ROUND_HALF_UP).toString());
        vo.setValue(df.format(new BigDecimal(array[0])));
        vo.setDecimal(array.length<=1?null:"."+array[1]);
        vo.setUnit("亿"+baseUnit);
        return vo;
    }

    public static SubtotalViewVO getSubtotalVOIncrease(String label, BigDecimal number){
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit(PERCENT);
            return vo;
        }
        BigDecimal percentage = number.multiply(new BigDecimal("100")).setScale(2);
        String[] array = decimalStrToArray(percentage.toString());
        vo.setValue(new BigDecimal(array[0]).compareTo(BigDecimal.ZERO) > 0 ?"+"+array[0]:array[0]);
        vo.setDecimal(array.length<=1?null:"."+array[1]);
        vo.setUnit(PERCENT);
        return vo;
    }

    public static SubtotalViewVO getSubtotalVODelta(String label, String baseUnit, BigDecimal number){
        DecimalFormat df = new DecimalFormat("###,###");
        int scale = 2;
        SubtotalViewVO vo = new SubtotalViewVO();
        vo.setLabel(label);
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            vo.setValue("0");
            vo.setDecimal(".00");
            vo.setUnit(baseUnit);
            return vo;
        }
        //小于10万
        if(number.abs().compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND)) < 0 ){
            String[] array = decimalStrToArray(number.toString());
            vo.setValue(df.format(new BigDecimal(array[0])));
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit(baseUnit);
            return vo;
        }
        //大于10万小于10亿
        if(number.abs().compareTo(new BigDecimal(BILLION)) < 0){
            String[] array = decimalStrToArray(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP).toString());
            vo.setValue(df.format(new BigDecimal(array[0])));
            vo.setDecimal(array.length<=1?null:"."+array[1]);
            vo.setUnit("万"+baseUnit);
            return vo;
        }
        String[] array = decimalStrToArray(number.divide(new BigDecimal(HUNDRED_MILLION),scale,BigDecimal.ROUND_HALF_UP).toString());
        vo.setValue(df.format(new BigDecimal(array[0])));
        vo.setDecimal(array.length<=1?null:"."+array[1]);
        vo.setUnit("亿"+baseUnit);
        return vo;
    }

    public static String[] decimalStrToArray(String str){
        String[] array = str.split("\\.");
        if(array.length == 0){
            return new String[]{str};
        }
        return array;
    }

    public static String getSimpleFormat(String baseUnit, BigDecimal number){
        //DecimalFormat df = JIAN.equals(baseUnit)?new DecimalFormat("###,###"):new DecimalFormat("###,###.##");
        DecimalFormat df = new DecimalFormat("###,##0.00");
        int scale = 2;
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            return "0.00";
        }
        return df.format(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP));

//        //小于10万
//        if(number.abs().compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND)) < 0 ){
//            return df.format(number);
//        }
//        //大于10万小于10亿
//        if(number.abs().compareTo(new BigDecimal(BILLION)) < 0){
//            return df.format(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP));
//        }
//        return df.format(number.divide(new BigDecimal(HUNDRED_MILLION),scale,BigDecimal.ROUND_HALF_UP));
    }

    public static String getSimpleFormat2(String baseUnit, BigDecimal number){
        DecimalFormat df = new DecimalFormat("###,##0.00");
        int scale = 2;
        if(number == null || number.compareTo(BigDecimal.ZERO)==0){
            return "0.00"+"万"+baseUnit;
        }
        return df.format(number.divide(new BigDecimal(TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP))+"万"+baseUnit;
    }

    public static void main(String[] args) {
        System.out.println(SubtotalViewVOUtil.getSimpleFormat2("",new BigDecimal("14000")));;
    }

}
